<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Product_Sale_Model extends MY_Model {
    public $entity_name = "Product_Sale_Entity";

    public function __construct() {
        parent::__construct();
        $this->load->model('entity/product_sale_entity', $this->entity_name);
    }

    public function countSale() {
        $this->db->from($this->table_product_sale_name . ' AS a');
        $this->db->join($this->table_product_name         . ' b', 'a.product_id=b.id');
        $this->db->join($this->table_product_details_name . ' c', 'a.product_id=c.product_id');
        $this->db->join($this->table_category_name        . ' d', 'b.cate_id=d.id');
        $this->db->join($this->table_shop_name            . ' e', 'b.shop_id=e.id');
        $this->db->join($this->table_user_name            . ' f', 'e.user_id=f.id');
        $this->db->where('b.active_flg' , 1);
        $this->db->where('c.is_avatar' , 1);
        $this->db->where('a.deleted_at'  , NULL);
        $this->db->where('b.deleted_at'  , NULL);
        $this->db->where('c.deleted_at'  , NULL);
        $this->db->where('d.deleted_at'  , NULL);
        $this->db->where('e.deleted_at'  , NULL);
        $this->db->where('f.deleted_at'  , NULL);
        return $this->db->count_all_results();
    }

    public function getLatestSale() {
        $select = "a.id, a.sale_flg, b.id as detail_id, a.title, a.slug, b.image,b.code, b.price_old, b.price_new, f.image_folder as shop_image_folder";
        $this->db->select($select, true);
        $this->db->from($this->table_product_name         . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name            . ' d', 'd.id=a.shop_id');
        $this->db->join($this->table_category_name        . ' e', 'a.cate_id=e.id');
        $this->db->join($this->table_user_name            . ' f', 'd.user_id=f.id');
        $this->db->where('a.active_flg', 1);
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.is_avatar', 1);
        $this->db->where('b.deleted_at', null);
        $this->db->where('a.sale_flg', ON);
        $this->db->where('e.deleted_at', null);
        $this->db->where('f.deleted_at', null);
        $this->db->order_by('a.created_at', 'DESC');
        $this->db->order_by('a.updated_at', 'DESC');
        $this->db->order_by('b.created_at', 'DESC');
        $this->db->order_by('b.updated_at', 'DESC');
        $this->db->limit(PRODUCT_LIMIT);
        return $this->db->get()->result($this->entity_name);
    }

    public function getSale($offset=0) {
        $this->load->model('entity/product_details_entity', 'Product_Details_Entity');
        $select = "a.id, b.id as detail_id, a.shop_id, a.title, b.description, a.slug,
        b.image, b.price_old, b.price_new,
        d.slug as shop_slug, d.title as shop_title, f.image_folder as shop_image_folder,
        f.phone";
        $this->db->select($select, true);
        $this->db->from($this->table_product_name         . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_product_sale_name . ' c', 'a.id=c.product_id');
        $this->db->join($this->table_shop_name            . ' d', 'd.id=a.shop_id');
        $this->db->join($this->table_category_name        . ' e', 'a.cate_id=e.id');
        $this->db->join($this->table_user_name            . ' f', 'd.user_id=f.id');
        $this->db->where('a.active_flg', 1);
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.is_avatar', 1);
        $this->db->where('b.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->where('e.deleted_at', null);
        $this->db->where('f.deleted_at', null);
        $this->db->order_by('a.created_at', 'DESC');
        $this->db->order_by('a.updated_at', 'DESC');
        $this->db->order_by('b.created_at', 'DESC');
        $this->db->order_by('b.updated_at', 'DESC');
        $this->db->limit(SALE_LIMIT);
        $this->db->offset($offset);
        $result = $this->db->get()->result('Product_Details_Entity');
        return $result;
    }

    public function getShopSale($shop_id) {
        $select = "a.id, a.title, a.slug, b.image, b.code, b.price_old, b.price_new";
        $this->db->select($select, true);
        $this->db->from($this->table_product_name         . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_product_sale_name . ' c', 'a.id=c.product_id');
        $this->db->where('a.shop_id', $shop_id);
        $this->db->where('a.active_flg', 1);
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.is_avatar', 1);
        $this->db->where('b.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->order_by('a.created_at', 'DESC');
        $this->db->order_by('a.updated_at', 'DESC');
        $this->db->order_by('b.created_at', 'DESC');
        $this->db->order_by('b.updated_at', 'DESC');
        $this->db->limit(PRODUCT_LIMIT);
        return $this->db->get()->result($this->entity_name);
    }
}
?>